class Describe{
    constructor(id,teaname,teatype,teaimg,teaprice,teadescribe){
        this.id=id;
        this.teaname=teaname;
        this.teatype=teatype;
        this.teaimg=teaimg;
        this.teaprice=teaprice;
        this.teadescribe=teadescribe;   
    }
}

window.onload = function () {
    let divobj = document.getElementById("content"); 
    $.ajax({
        type: "post",//请求方式   get  post
        url: "/js/首页回显",//接口地址
        data: null,//是否携带参数
        dataType: "json",//数据类型
        success: function (teaString) { //回调函数   teaString表示后台接口返回的数据
            let arr = JSON.parse(teaString);
            let s = "";
            for (let i = arr.length-1; i > arr.length -7; i--) {
                s+=`<div class="part" id="part"  onclick="describe(${arr[i].id})">`
                s+=`<div class="area" id="area">`
                s+=`<div class="imgdiv">`
                s+=`<img src="../img/tea-img/${arr[i].teaimg}" alt="">`
                s+=`</div>`
                s+=`<div class="font1">${arr[i].teaname}</div>`
                s+=`<div class="bigbox">`
                s+=`<div class="box1"></div>`
                s+=`<div class="box2"></div>`
                s+=`</div>`
                s+=`<div class="font2">￥${arr[i].teaprice}</div>`
                s+=`</div>`
                s+=`</div>`
                
            }
            s+=`<div class="box">`
            s+=`<a href="cy.html"><p style="font-size: 40px; color: white;text-align: center;">→</p></a>`
            s+=`</div>`  
            divobj.innerHTML = s;  
            let partobj=document.getElementsByClassName("part");
                window.onscroll=function(){
                let top = document.documentElement.scrollTop;
                if(top>550){ 
                    partobj[0].style.animation = "scroll1 1s linear forwards .1s";
                    partobj[1].style.animation = "scroll1 1s linear forwards .2s";
                    partobj[2].style.animation = "scroll1 1s linear forwards .3s";
                    partobj[3].style.animation = "scroll1 1s linear forwards .4s";
                    partobj[4].style.animation = "scroll1 1s linear forwards .5s";
                    partobj[5].style.animation = "scroll1 1s linear forwards .6s";
                }  
            } 
        }
    }) 
    //强制登录
    //获取cookie
    let cookieStr= document.cookie;
    let cookirArr=cookieStr.split(";");
    let bool=false;
    let username;;
    for(let i=0;i<cookirArr.length;i++){
            //去除空格
        let str=cookirArr[i].trim();

        let key="username=";
        if(str.startsWith(key)){
            bool=true;
            username=str.substring(key.length,str.length);
            break;
        }
    }

    //判断bool标记值
    if(bool){
        console.log(username);
    }else{
        window.location.href="login.html"
    }      
}

function describe(id){
    let teaString=localStorage.Tea;
    let teaArr=JSON.parse(teaString);
    for(let i=0;i<teaArr.length;i++){
        if(id==teaArr[i].id){
            let newtea=new Describe(teaArr[i].id,teaArr[i].teaname,teaArr[i].teatype,teaArr[i].teaimg,teaArr[i].teaprice,teaArr[i].teadescribe);
            let arr=[newtea];
            localStorage.Describe=JSON.stringify(arr);
        }
    }
    location.href="茶叶简介.html";
}

